package com.zxc.userservice.handler;

import com.zxc.oj.model.enums.UserRoleEnum;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserRoleEnumTypeHandler implements TypeHandler<UserRoleEnum> {

    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, UserRoleEnum userRoleEnum, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, userRoleEnum.getValue());
    }


    @Override
    public UserRoleEnum getResult(ResultSet resultSet, String s) throws SQLException {
        String value = resultSet.getString(s);
        return UserRoleEnum.getEnumByValue(value);
    }

    @Override
    public UserRoleEnum getResult(ResultSet resultSet, int i) throws SQLException {
        String value = resultSet.getString(i);
        return UserRoleEnum.getEnumByValue(value);
    }

    @Override
    public UserRoleEnum getResult(CallableStatement callableStatement, int i) throws SQLException {
        String value = callableStatement.getString(i);
        return UserRoleEnum.getEnumByValue(value);
    }
}